package com.ybbase.framework.controller.flow.base;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ybbase.framework.base.model.ResponseBean;
import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.common.util.DateUtil;
import com.ybbase.framework.model.po.flow.FlowActiviti;
import com.ybbase.framework.model.po.system.User;
import com.ybbase.framework.service.system.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cg
 * @date 2019-08-27 09:09
 * @DEC 流程节点管理
 */
@RestController
@RequestMapping("/flowactiviti")
@Api(value = "流程节点管理",tags = "流程节点管理")
public class FlowActivitiController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlowActivitiController.class);

    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    private UserService userService;
    /**
     * 定义流程引擎
     */
    @Resource
    private ProcessEngine processEngine;

    /**
     * 根据流程实例编码获取流程实例的节点信息
     * @author cg
     * @date 2019-08-27 14:08
     * @param processInstanceId 流程实例编码
     * @return
     * @throws
     * @since
     */
    @GetMapping("/getFlowActivitis")
    @ApiOperation(value = "根据流程实例编码获取流程实例的节点信息",notes="根据流程实例编码获取流程实例的节点信息",response = ResponseBean.class)
    @ApiImplicitParam(name="processInstanceId",value="流程实例编码", required=true, dataType = "String")
    public ResponseBean getFlowActivitis(String processInstanceId){
        //查询出已经发生的节点的信息
        List<HistoricActivityInstance> list = processEngine.getHistoryService()
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId).orderByHistoricActivityInstanceStartTime().asc().list();
        List<FlowActiviti> flowActivitiList = new ArrayList<>();
        try {
            if(list!=null&&list.size()>0){
                for(HistoricActivityInstance ha:list){
                    FlowActiviti flowActiviti = new FlowActiviti();
                    flowActiviti.setActId(ha.getActivityId());
                    flowActiviti.setActivitiName(ha.getActivityName());
                    User user = userService.getById(Long.parseLong(StringUtils.isNotEmpty(ha.getAssignee())?ha.getAssignee():"0"));
                    flowActiviti.setAssignee(user!=null?user.getName():"");
                    flowActiviti.setStartTime(DateUtil.getTime(ha.getStartTime() != null ? ha.getStartTime() : new Date()));
                    flowActiviti.setEndTime(DateUtil.getTime(ha.getEndTime()!= null ? ha.getEndTime() : new Date()));
                    flowActiviti.setTaskId(ha.getTaskId());
                    flowActivitiList.add(flowActiviti);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            LOGGER.error(e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"根据流程实例编码获取流程实例的节点信息异常",null);
        }
        return new ResponseBean(CommonResultCode.SUCCESS.getCode(),CommonResultCode.SUCCESS.getMsg(),flowActivitiList);
    }


}
